package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.youdianda.dto.ConfigDto;
import com.itheima.youdianda.dto.ConfigDto1;
import com.itheima.youdianda.entity.SysConfig;
import com.itheima.youdianda.mapper.SysConfigMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.service.SysConfigService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {


    private static HashMap<String,String> configName = new HashMap<>();
    static {
        configName.put("base","基础配置");
        configName.put("system","系统设置");
        configName.put("custom","其他设置");
    }


    @Override
    public Map<String, Object> all() {
        Map<String, Object> map = new HashMap<>();
        //配置树查询
        ArrayList<ConfigDto> list = new ArrayList<>();
        //配置参数
        ArrayList<SysConfig> allData = new ArrayList<>();
        //表单参数
        HashMap<String,Object> configFrom = new HashMap<>();
        //表单规则
        HashMap<String,String> configRules = new HashMap<>();

        QueryWrapper<SysConfig> lqw = new QueryWrapper<>();
        lqw.lambda().select(SysConfig::getTab_value,SysConfig::getGroup_value);
        lqw.lambda().groupBy(SysConfig::getTab_value);
        //获取一级属性列表
        List<SysConfig> tabList = this.list(lqw);
        //遍历一级属性,获取下级属性
        for (SysConfig tabConfig : tabList) {
            String key = tabConfig.getTab_value();
            System.out.println("tab_value = "+key);
            //创建一级属性对象
            ConfigDto configDto = new ConfigDto();
            configDto.setKey(key);
            configDto.setName(configName.get(key));
            //根据一级属性对象查找二级属性对象
            configDto = getChildren(configDto,allData,configFrom,configRules);
            //将一级属性添加到列表
            list.add(configDto);
        }
        //获取configfrom


        //HashMap<String, Object> map = new HashMap<>();
        map.put("allData",allData);
        map.put("configForm",configFrom);
        map.put("configRules",configRules);
        map.put("list",list);
        return map;
    }

    private ConfigDto getChildren(ConfigDto configDto,ArrayList allData,HashMap configFrom,HashMap configRules){
        //获取一级属性名特征 Tab_value
        QueryWrapper<SysConfig> lqw = new QueryWrapper<>();
        System.out.println("configDto.getKey() = " + configDto.getKey());
        //根据一级属性特征 获取二级属性列表
        lqw.lambda().select(SysConfig::getGroup_value);
        lqw.lambda().eq(SysConfig::getTab_value,configDto.getKey());
        lqw.lambda().groupBy(SysConfig::getGroup_value);
        List<SysConfig> list = this.list(lqw);
        //遍历二级属性列表,查询对应三级属性
        for (SysConfig config : list) {
            //创建dto 装载三级属性属性
            ConfigDto1 dto = new ConfigDto1();
            dto.setName(config.getGroup_value());
            ConfigDto1 data = getData(dto, allData,configFrom,configRules);
            //将三级属性封装到二级属性的子列表中
            configDto.addChildren(data);
        }
        //根据二级属性去查三级属性
        //getData(configDto,allData,configFrom,configRules);
        return configDto;
    }

    private ConfigDto1 getData(ConfigDto1 config,ArrayList allData,HashMap configFrom,HashMap configRules) {
        LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getGroup_value,config.getName())
                .orderByAsc(SysConfig::getId);
        System.out.println("Config_value = " + config.getName());
        List<SysConfig> list = this.list(lqw);
        for (SysConfig config1 : list) {
            String  value = (String) config1.getConfig_value();
            if (value != null && value != ""){
                try {
                    ArrayList<Map> objects = new ArrayList<>();
                    String[] split = value.split("&");
                    for (String s : split) {
                        String[] split1 = s.split("=");
                        HashMap hashMap = new HashMap();
                        hashMap.put("label",split1[0]);
                        hashMap.put("value",split1[1]);
                        objects.add(hashMap);
                    }
                    config1.setConfig_value(objects);
                } catch (Exception e) {

                }
            }

        }
        config.setData(list);
        allData.addAll(list);
        for (SysConfig con : list) {
            configFrom.put(con.getAttr_key(),con.getAttr_value());
            if (con.getValidator() != null && con.getValidator() != "" && !con.getValidator().isEmpty()){
                configRules.put(con.getAttr_key(),con.getValidator());
            }
        }
        return config;
    }
}
